package RentCarProject;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Random;

public class ForgetPasswordFrame extends JFrame {
    JLabel usernamelabel,passwordlabel1,passwordlabel2,yzmlabel1,yzmlabel2,forgetpasswordlabel;
    JTextField usernameField,passwordField1,passwordField2,yzmlabelField;
    MyButton updatebutton,exitbutton;
    JButton yzmbutton;
    String yzm;
    public ForgetPasswordFrame(){
        setLayout(null);
        setTitle("忘记密码");
        setSize(500,500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        forgetpasswordlabel=new JLabel(new ImageIcon("src/picture/forgetpassordbg.png"));
        forgetpasswordlabel.setBounds(0,0,500,500);
        forgetpasswordlabel.setOpaque(false);
        usernamelabel=new JLabel("用户名：");
        usernamelabel.setForeground(Color.white);
        usernamelabel.setFont(new Font("宋体", Font.BOLD,15));
        usernamelabel.setBounds(100,55,100,30);
        passwordlabel1=new JLabel("新密码：");
        passwordlabel1.setForeground(Color.white);
        passwordlabel1.setFont(new Font("宋体", Font.BOLD,15));
        passwordlabel1.setBounds(100,115,100,30);
        passwordlabel2=new JLabel("确认密码：");
        passwordlabel2.setForeground(Color.white);
        passwordlabel2.setFont(new Font("宋体", Font.BOLD,15));
        passwordlabel2.setBounds(100,175,100,30);
        yzmlabel1=new JLabel();
        yzm=yzmCreate();
        yzmlabel1.setText(yzm);
        yzmlabel1.setForeground(Color.white);
        yzmlabel1.setBounds(180,230,150,50);
        yzmlabel1.setFont(new Font("宋体", Font.BOLD,35));
        yzmlabel2=new JLabel("验证码：");
        yzmlabel2.setForeground(Color.white);
        yzmlabel2.setFont(new Font("宋体", Font.BOLD,15));
        yzmlabel2.setBounds(100,295,100,30);

        usernameField=new JTextField();
        usernameField.setBounds(200,55,200,30);
        passwordField1=new JPasswordField();
        passwordField1.setBounds(200,115,200,30);
        passwordField2=new JPasswordField();
        passwordField2.setBounds(200,175,200,30);
        yzmlabelField=new JTextField();
        yzmlabelField.setBounds(200,295,200,30);

        yzmbutton=new JButton("换一个");
        yzmbutton.setForeground(Color.white);
        yzmbutton.setBounds(280,235,80,40);
        yzmbutton.setFont(new Font("宋体", Font.PLAIN,10));
        yzmbutton.setContentAreaFilled(false);
        yzmbutton.setBorder(null);
        yzmbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                yzm=yzmCreate();
                yzmlabel1.setText(yzm);
            }
        });
        updatebutton=new MyButton("确定");
        updatebutton.setFont(new Font("宋体",Font.BOLD,15));
        updatebutton.setBounds(100,380,100,40);
        updatebutton.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if(passwordField1.getText().equals(passwordField2.getText())){
                            {
                                if(yzm.equals(yzmlabelField.getText())){
                                    try {
                                        if (searchUsername(usernameField.getText())) {
                                            if (resetPassword(usernameField.getText(), passwordField1.getText())) {
                                                JOptionPane.showMessageDialog(null, "密码重置成功！");
                                            } else {
                                                JOptionPane.showMessageDialog(null, "密码重置失败！");
                                            }
                                        } else {
                                            JOptionPane.showMessageDialog(null, "账号不存在！");
                                        }
                                    } catch (Exception ex) {
                                        throw new RuntimeException(ex);
                                    }
                                }else{
                                    JOptionPane.showMessageDialog(null,"验证码错误！");
                                }
                            }
                        }else{
                            JOptionPane.showMessageDialog(null,"两次密码不一致！");
                        }
                    }
                });
        exitbutton=new MyButton("返回登录");
        exitbutton.setFont(new Font("宋体",Font.BOLD,15));
        exitbutton.setBounds(300,380,100,40);
        exitbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginUI();
                dispose();
            }
        });

        add(usernamelabel);
        add(usernameField);
        add(passwordlabel1);
        add(passwordField1);
        add(passwordlabel2);
        add(passwordField2);
        add(yzmlabel1);
        add(yzmbutton);
        add(yzmlabel2);
        add(yzmlabelField);
        add(updatebutton);
        add(exitbutton);
        add(forgetpasswordlabel);
        setVisible(true);
    }

    public String  yzmCreate(){
        Random rand = new Random();
        String str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        StringBuilder yzm=new StringBuilder();
        for(int i=0;i<6;i++){
            int index = rand.nextInt(str.length());
            char ch=str.charAt(index);
            yzm.append(ch);
        }
        return yzm.toString();
    }

    public boolean searchUsername(String username) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="select * from user where username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,username);
        ResultSet resultSet=preparedStatement.executeQuery();
        if(resultSet.next()){
            resultSet.close();
            preparedStatement.close();
            connection.close();
            return true;
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return false;
    }

    public boolean resetPassword(String username,String password) throws Exception {
        Properties properties=new Properties();
        properties.load(new FileInputStream("src/druid.properties"));
        DataSource dataSource= DruidDataSourceFactory.createDataSource(properties);
        Connection connection=dataSource.getConnection();
        String sql="update user set password=? where username=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);
        preparedStatement.setString(1,password);
        preparedStatement.setString(2,username);
        int a=preparedStatement.executeUpdate();
        preparedStatement.close();
        connection.close();
        return a>0?true:false;
    }

    public static void main(String[] args) {
        new ForgetPasswordFrame();
    }
}
